package org.csu.jpetstore_users_backend_pj.util;

//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class EncryptUtil {

    private final BCryptPasswordEncoder passwordEncoder;

    // 通过构造函数注入
    public EncryptUtil(BCryptPasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }

    /**
     * 加密密码
     * @param password 原始密码
     * @return 加密后的哈希值
     */
    public String encryptPwd(String password) {
        return passwordEncoder.encode(password);
    }

    /**
     * 验证密码
     * @param password 用户输入的密码
     * @param encryptedPwd 已加密的哈希值
     * @return 是否匹配
     */
    public boolean verifyPwd(String password, String encryptedPwd) {
        return passwordEncoder.matches(password, encryptedPwd);
    }
}

